package mz.matafe.bolao

import grails.test.*

class CupTests extends GrailsUnitTestCase {

    def cup
    def user

    protected void setUp() {
        super.setUp()
        // Set up default cup
        cup = new Cup(year: 2010, venue: 'Africa do Sul')
        user = new User(username: 'mferraz', password: '123456',
            firstname: 'Mauricio', surname: 'Ferraz',
            email: 'matafe@gmail.com', contactNumber: '+258846819000' )
        mockForConstraintsTests(Cup, [cup])
        mockForConstraintsTests(User, [user])
    }

    protected void tearDown() {
        super.tearDown()
    }

    /**
     * Testing toString() method
     */
    void testToString() {
        assertEquals 'Africa do Sul - 2010' , cup.toString()
    }

    /**
     * Testing the nullable constraints
     */
    void testConstraintsNullable() {
        cup = new Cup()
        assertFalse cup.validate()
        assertEquals 4, cup.errors.errorCount
        assertEquals 'The year is null','nullable', cup.errors['year']
        assertEquals 'The venue is null', 'nullable', cup.errors['venue']
        assertEquals 'The createdBy is null', 'nullable', cup.errors['createdBy']
        assertEquals 'The updatedBy is null', 'nullable', cup.errors['updatedBy']
        cup = new Cup(year: 2014, venue: 'Brasil', createdBy:user,
            updatedBy: user)
        cup.validate()
        print cup.errors
        assertTrue cup.validate()
    }

    /**
     * Testing the unique constraints
     */
    void testConstraintsUnique() {
        cup = new Cup(year: 2010, venue: 'Africa do Sul')
        assertFalse cup.validate()
        assertEquals 'The year must be unique', 'unique', cup.errors['year']
        cup = new Cup(year: 2014, venue: 'Brasil', createdBy: user,
            updatedBy: user)
        assertTrue cup.validate()
    }


   /**
     * Testing the blank constraints
     */
    void testConstraintsBlank() {
        cup = new Cup(year: '', venue:'')
        assertFalse cup.validate()
        assertEquals 4, cup.errors.errorCount
        assertEquals 'The year is blank','nullable', cup.errors['year']
        assertEquals 'The venue is blank','blank', cup.errors['venue']
        assertEquals 'The createdBy is blank','nullable', cup.errors['createdBy']
        assertEquals 'The updatedBy is blank','nullable', cup.errors['updatedBy']
        cup = new Cup(year: 2014, venue: 'Brasil', createdBy: user,
            updatedBy: user)
        assertTrue cup.validate()
    }

    /**
     * Testing the size constraints
     */
    void testConstraintsSize() {
        cup = new Cup(year: 2014, venue: 'B'*51, createdBy:user,
            updatedBy: user)
        assertFalse cup.validate()
        assertEquals 'The venue has more than 50 characters','size', cup.errors['venue']
        cup.venue = 'Brasil'
        cup.validate()
        println cup.errors
        assertTrue cup.validate()
    }

    /**
     * Testing the validator constraints
     */
    void testConstraintsValidator() {
        cup = new Cup(year: 14, venue: 'Brasil', createdBy:user,
            updatedBy: user)
        assertFalse cup.validate()
        assertEquals 'The year has less than 4 digit','validator', cup.errors['year']
        cup.year = 12345
        assertFalse cup.validate()
        assertEquals 'The year has more than 4 digit','validator', cup.errors['year']
        cup.year = 2014
        cup.validate()
        println cup.errors
        assertTrue cup.validate()
    }

    
}
